package edu.sysu.demo.petrabbit;

import static edu.sysu.demo.petrabbit.config.RabbitConfig.BUFFER_EXCHANGE_NAME;
import static edu.sysu.demo.petrabbit.config.RabbitConfig.BUFFER_EXCHANGE_ROUTING_KEY;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Publisher {

  private static final long DEFAULT_BUFFER_DELAY = 5000;

  private RabbitTemplate rabbitTemplate;

  @Autowired
  public Publisher(RabbitTemplate rabbitTemplate) {
    this.rabbitTemplate = rabbitTemplate;
  }

  public void send(Object message, long delayMillis) {
    rabbitTemplate.convertAndSend(
        BUFFER_EXCHANGE_NAME,
        BUFFER_EXCHANGE_ROUTING_KEY,
        message,
        ExpirationMessagePostProcessor.create(delayMillis));
  }

  public void send(Object message) {
    send(message, DEFAULT_BUFFER_DELAY);
  }
}

